package com.xdtech.play.init;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.xdtech.common.service.impl.BaseService;
import com.xdtech.common.utils.EmptyUtil;
import com.xdtech.core.init.SysInitOperation;
import com.xdtech.core.model.BaseModel;
import com.xdtech.play.util.PlayContants;
import com.xdtech.sys.model.CodeValue;
import com.xdtech.sys.model.DictionaryCode;
import com.xdtech.sys.model.MenuFunction;
import com.xdtech.sys.model.Params;

public class PlayInit implements SysInitOperation {
	Log log = LogFactory.getLog(PlayInit.class);

	@Override
	public void initingToDb(BaseService<BaseModel> baseService) {
		List params = (List) baseService.findByHql("from Params where code=? and codeValue=?", "PlayInit","true");
		log.info("游戏模块数据库初始化......");
		if (EmptyUtil.isEmpty(params)) {
			initData(baseService);
			Params p = new Params("PlayInit", "true", "游戏模块数据库初始化标记");
			baseService.save(p);
			log.info("游戏模块数据库初始化......结束");
		}else {
			log.info("游戏模块数据库已经初始化......完毕");
		}
		
	}

	private void initData(BaseService<BaseModel> baseService) {
		MenuFunction m1 = new MenuFunction();
		m1.setIconName("icon-save");
		m1.setNameCN("娱乐管理");
		m1.setOperationCode("play-manage");
		m1.setPageUrl(null);
		baseService.save(m1);
		
		MenuFunction m11 = new MenuFunction();
		m11.setIconName("icon-save");
		m11.setNameCN("玩家账号");
		m11.setOperationCode("play-member-manage");
		m11.setPageUrl("playMember.do?playMember");
		m11.setParent(m1);
		baseService.save(m11);
		
		MenuFunction m12 = new MenuFunction();
		m12.setIconName("icon-save");
		m12.setNameCN("娱乐中心");
		m12.setOperationCode("play-center-manage");
		m12.setPageUrl("playCenter.do?playCenter");
		m12.setParent(m1);
		baseService.save(m12);
		
		
		//数据字典
		DictionaryCode dictionaryCode = new DictionaryCode("PAY_TYPE","支付方式");
		baseService.save(dictionaryCode);
		baseService.save(new CodeValue(PlayContants.PAY_ZFB,"支付宝",1,dictionaryCode));
		baseService.save(new CodeValue(PlayContants.PAY_WX,"微信",2,dictionaryCode));
		baseService.save(new CodeValue(PlayContants.PAY_YL,"银联",3,dictionaryCode));
		
		dictionaryCode = new DictionaryCode("TRADE_TYPE","支付方式");
		baseService.save(dictionaryCode);
		baseService.save(new CodeValue(PlayContants.TRADE_IN,"充值",1,dictionaryCode));
		baseService.save(new CodeValue(PlayContants.TRADE_OUT,"提取",2,dictionaryCode));
	}

	@Override
	public void initingToCache(BaseService<BaseModel> baseService) {
		
	}

	@Override
	public void initingToWebApplication(BaseService<BaseModel> baseService) {
		
	}

	@Override
	public int getInitOrder() {
		return 999;
	}
}
